package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ConstructAdapter;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.databinding.ActivitySelPicBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelPicActivity extends BaseAc<ActivitySelPicBinding> {

    public static int selPosition;
    private SelPictureAdapter selPictureAdapter;
    private List<SelectMediaEntity> listShow = new ArrayList<>();
    private String selPath = "";
    private int oldPosition = 0;

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_pic;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivSelPicBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivSelPicUpload.setOnClickListener(this);
        mDataBinding.rvSelPicList.setLayoutManager(new GridLayoutManager(mContext, 3));
        selPictureAdapter = new SelPictureAdapter();
        mDataBinding.rvSelPicList.setAdapter(selPictureAdapter);
        selPictureAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSelPicUpload:
                if (selPath.isEmpty()) {
                    ToastUtils.showShort(R.string.please_sel_pic);
                    return;
                }
                ConstructActivity.imgPath = selPath;
                ConstructActivity.selPosition=selPosition;
                startActivity(ConstructActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        selPictureAdapter.getItem(oldPosition).setChecked(false);
        selPictureAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        selPictureAdapter.getItem(position).setChecked(true);
        selPictureAdapter.notifyItemChanged(position);
        selPath = selPictureAdapter.getItem(position).getPath();
    }

    @Override
    protected void initData() {
        getPermission();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getString(R.string.get_storage_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                getData();
            }

            @Override
            public void onDenied(boolean needGoSettings) {
                mDataBinding.rvSelPicList.setVisibility(View.GONE);
                mDataBinding.llSelPicNoData.setVisibility(View.VISIBLE);
            }
        }).request();
    }

    //获取视频和图片数据
    private void getData() {
        listShow.clear();
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    listShow.addAll(selectMediaEntities);
                    selPictureAdapter.setList(listShow);
                    mDataBinding.rvSelPicList.setVisibility(View.VISIBLE);
                    mDataBinding.llSelPicNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvSelPicList.setVisibility(View.GONE);
                    mDataBinding.llSelPicNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}